﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Threading;
using Microsoft.Win32;
using System.Windows.Interop;



namespace VideoArchivePreview
{
    /// <summary>
    /// Interaction logic for Config_frm.xaml
    /// </summary>
    public partial class Preview_frm : Window
    {
        bool move = false;
        private System.Drawing.Point pt = new System.Drawing.Point();
        private bool mediaPlayerIsPlaying = false;
        private bool userIsDraggingSlider = false;
        Boolean doubleclick = true;
        public Boolean widen = false;


        public Preview_frm()
        {
            InitializeComponent();
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += timer_Tick;
            timer.Start();

        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if ((mplayer.Source != null) && (mplayer.NaturalDuration.HasTimeSpan) && (!userIsDraggingSlider))
            {
                sliProgress.Minimum = 0;
                sliProgress.Maximum = mplayer.NaturalDuration.TimeSpan.TotalSeconds;
                sliProgress.Value = mplayer.Position.TotalSeconds;
            }
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
          
           /* double leftpos = this.Left;
            while (this.Left < leftpos - this.Width + 50)
            {
                this.Left = this.Left - 10;
                Thread.Sleep(5);
            }*/

            foreach (Window wd in Application.Current.Windows)
            {
                if (wd.Title.Equals("MainWindow"))
                {
                    wd.Topmost = true;
                    foreach (Button bt in FindVisualChildren<Button>(wd))
                    {
                        if (bt.Name.Equals("button6"))
                        {
                            bt.Visibility = Visibility.Visible;
                        }
                    }
                    wd.Owner = null;
                    
                }
            }

            Close();
           Functionality.orderWindows();
        }

        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (move && !doubleclick)
            {
                this.Left += (System.Windows.Forms.Control.MousePosition.X - this.pt.X);
                this.Top += (System.Windows.Forms.Control.MousePosition.Y - this.pt.Y);
                Window main = null;
                Window cf = null;
                Window tm = null;
                foreach (Window wd in Application.Current.Windows)
                {
                    if (wd.Title.Equals("MainWindow"))
                    {
                        main = wd;
                    }
                    if (wd.Title.Equals("Config_frm"))
                    {
                        cf = wd;
                    }
                    if (wd.Title.Equals("TC_frm"))
                    {
                        tm = wd;
                    }
                    
                }
                if (!widen)
                {
                    if (main != null)
                    {
                        main.Left = this.Left - main.Width + 50;
                        main.Top = this.Top;
                    }
                    if (cf != null)
                    {
                        cf.Left = this.Left - main.Width - cf.Width + 100;
                        cf.Top = this.Top;
                    }
                    if (tm != null)
                    {
                        tm.Left = this.Left - main.Width - tm.Width + 100;
                        tm.Top = this.Top;
                    }
                    main.Topmost = true;
                }
                else
                {
                    if (main != null)
                    {
                        main.Left = this.Left;
                        main.Top = this.Top;
                    }
                    if (cf != null)
                    {
                        cf.Left = this.Left - cf.Width + 50;
                        cf.Top = this.Top;
                    }
                    if (tm != null)
                    {
                        tm.Left = this.Left- tm.Width + 50;
                        tm.Top = this.Top;
                    }
                    main.Topmost = true;
                }
                this.pt = System.Windows.Forms.Control.MousePosition;
            }
            if (doubleclick) { doubleclick = false; }
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            move = true;
            this.pt = System.Windows.Forms.Control.MousePosition;
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            move = false;
        }

        private void Window_MouseLeave(object sender, MouseEventArgs e)
        {
            move = false;
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
        }

        private void textBox3_KeyUp(object sender, KeyEventArgs e)
        {
        }

        private void textBox4_KeyUp(object sender, KeyEventArgs e)
        {
        }

        private void textBox5_KeyUp(object sender, KeyEventArgs e)
        {
        }

        private void sliProgress_DragStarted(object sender, DragStartedEventArgs e)
        {
            userIsDraggingSlider = true;
        }

        private void sliProgress_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            userIsDraggingSlider = false;
            mplayer.Position = TimeSpan.FromSeconds(sliProgress.Value);
        }

        private void sliProgress_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            lblProgressStatus.Text = TimeSpan.FromSeconds(sliProgress.Value).ToString(@"hh\:mm\:ss");
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            mplayer.Play();
            mediaPlayerIsPlaying = true;

        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            mplayer.Pause();

        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            mplayer.Stop();
            mediaPlayerIsPlaying = false;

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
                var hwndTarget = hwndSource.CompositionTarget;
                hwndTarget.RenderMode = RenderMode.SoftwareOnly;
            }
            catch (Exception ex)
            {
                //Log.ErrorException(ex.Message, ex);
            }
        }

        private void Window_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            doubleclick = true;
            Window main = null;
            Window cf = null;
            Window tm = null;
            foreach (Window wd in Application.Current.Windows)
            {
                if (wd.Title.Equals("MainWindow"))
                {
                    main = wd;
                    main.Owner = null;
                    main.Topmost = false;
                }
                if (wd.Title.Equals("Config_frm"))
                {
                    cf = wd;
                    cf.Owner = null;
                    cf.Topmost = false;
                }
                if (wd.Title.Equals("TC_frm"))
                {
                    tm = wd;
                    tm.Owner = null;
                    tm.Topmost = false;
                }

            }
            this.Owner = null;
            this.Topmost = false;

            
            double leftpos = this.Left;
            if (!widen)
            {
                this.Left = main.Left;
                this.Width += main.Width;

                if (cf != null)
                {
                    // this.Owner = main;
                    main.Owner = cf;
                    this.Owner = main;
                    cf.Topmost = true;
                }
                if (tm != null)
                {
                    // this.Owner = main;
                    main.Owner = tm;
                    this.Owner = main;
                    tm.Topmost = true;
                }
                this.Topmost = true;
                this.Activate();
               
                widen = true;
            }
            else
            {
                this.Left = this.Left + (this.Width - 300) - 50;
                this.Width = 300;
      
                if (cf != null)
                {
                    cf.Owner = null;
                    cf.Topmost = true;
                    main.Owner = this;
                    main.Activate();
                    main.Topmost = true;
                }
                else if (tm != null)
                {
                    // this.Owner = main;
                    tm.Topmost = true;
                    tm.Owner = null;
                    main.Owner = this;
                    main.Activate();
                    main.Topmost = true;
                }
                else
                {
                    main.Owner = this;
                    main.Activate();
                    main.Topmost = true;
                }
                

                widen = false;
            }
            this.Topmost = true;
            Functionality.orderWindows();
        }

        private void goToPlay(string intime)
        {
            foreach (Window wd in Application.Current.Windows)
            {
                if (wd.Title.Equals("Preview_frm"))
                {
                    try
                    {
                        ((Preview_frm)wd).mplayer.Position = TimeSpan.Parse(intime);
                    }
                    catch (Exception ex) { }
                }
            }
        }

        private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            foreach (Window wd in Application.Current.Windows)
            {
                if (wd.Title.Equals("TC_frm"))
                {
                    if (e.Key == Key.Space)
                    {
                        if (((TC_frm)wd).isikirtimas_15.IsFocused)
                        { ((TC_frm)wd).isikirtimas_1.Focus(); goToPlay(((TC_frm)wd).isikirtimas_1.Text); }
                        else
                            if (((TC_frm)wd).isikirtimas_14.IsFocused)
                            { ((TC_frm)wd).isikirtimas_15.Focus(); goToPlay(((TC_frm)wd).isikirtimas_15.Text); }
                            else
                                if (((TC_frm)wd).isikirtimas_13.IsFocused)
                                { ((TC_frm)wd).isikirtimas_14.Focus(); goToPlay(((TC_frm)wd).isikirtimas_14.Text); }
                                else
                                    if (((TC_frm)wd).isikirtimas_12.IsFocused)
                                    { ((TC_frm)wd).isikirtimas_13.Focus(); goToPlay(((TC_frm)wd).isikirtimas_13.Text); }
                                    else
                                        if (((TC_frm)wd).isikirtimas_11.IsFocused)
                                        { ((TC_frm)wd).isikirtimas_12.Focus(); goToPlay(((TC_frm)wd).isikirtimas_12.Text); }
                                        else
                                            if (((TC_frm)wd).isikirtimas_10.IsFocused)
                                            { ((TC_frm)wd).isikirtimas_11.Focus(); goToPlay(((TC_frm)wd).isikirtimas_11.Text); }
                                            else
                                                if (((TC_frm)wd).isikirtimas_9.IsFocused)
                                                { ((TC_frm)wd).isikirtimas_10.Focus(); goToPlay(((TC_frm)wd).isikirtimas_10.Text); }
                                                else
                                                    if (((TC_frm)wd).isikirtimas_8.IsFocused)
                                                    { ((TC_frm)wd).isikirtimas_9.Focus(); goToPlay(((TC_frm)wd).isikirtimas_9.Text); }
                                                    else
                                                        if (((TC_frm)wd).isikirtimas_7.IsFocused)
                                                        { ((TC_frm)wd).isikirtimas_8.Focus(); goToPlay(((TC_frm)wd).isikirtimas_8.Text); }
                                                        else
                                                            if (((TC_frm)wd).isikirtimas_6.IsFocused)
                                                            { ((TC_frm)wd).isikirtimas_7.Focus(); goToPlay(((TC_frm)wd).isikirtimas_7.Text); }
                                                            else
                                                                if (((TC_frm)wd).isikirtimas_5.IsFocused)
                                                                { ((TC_frm)wd).isikirtimas_6.Focus(); goToPlay(((TC_frm)wd).isikirtimas_6.Text); }
                                                                else
                                                                    if (((TC_frm)wd).isikirtimas_4.IsFocused)
                                                                    { ((TC_frm)wd).isikirtimas_5.Focus(); goToPlay(((TC_frm)wd).isikirtimas_5.Text); }
                                                                    else
                                                                        if (((TC_frm)wd).isikirtimas_3.IsFocused)
                                                                        { ((TC_frm)wd).isikirtimas_4.Focus(); goToPlay(((TC_frm)wd).isikirtimas_4.Text); }
                                                                        else
                                                                            if (((TC_frm)wd).isikirtimas_2.IsFocused)
                                                                            { ((TC_frm)wd).isikirtimas_3.Focus(); goToPlay(((TC_frm)wd).isikirtimas_3.Text); }
                                                                            else
                                                                                if (((TC_frm)wd).isikirtimas_1.IsFocused)
                                                                                { ((TC_frm)wd).isikirtimas_2.Focus(); goToPlay(((TC_frm)wd).isikirtimas_2.Text); }
                        if (((!((TC_frm)wd).isikirtimas_15.IsFocused) && (!((TC_frm)wd).isikirtimas_14.IsFocused) && (!((TC_frm)wd).isikirtimas_13.IsFocused) && (!((TC_frm)wd).isikirtimas_12.IsFocused) &&
                             (!((TC_frm)wd).isikirtimas_11.IsFocused) && (!((TC_frm)wd).isikirtimas_10.IsFocused) && (!((TC_frm)wd).isikirtimas_9.IsFocused) && (!((TC_frm)wd).isikirtimas_8.IsFocused) &&
                            (!((TC_frm)wd).isikirtimas_7.IsFocused) && (!((TC_frm)wd).isikirtimas_6.IsFocused) && (!((TC_frm)wd).isikirtimas_5.IsFocused) && (!((TC_frm)wd).isikirtimas_4.IsFocused) &&
                            (!((TC_frm)wd).isikirtimas_3.IsFocused) && (!((TC_frm)wd).isikirtimas_2.IsFocused) && (!((TC_frm)wd).isikirtimas_1.IsFocused)))
                        { ((TC_frm)wd).isikirtimas_1.Focus(); goToPlay(((TC_frm)wd).isikirtimas_1.Text); }
                        Functionality.orderWindows();
                    }
                }
            }

           
        }

        

       /* public void orderWindows()
        {
            bool widen = false;
            Window mn = getWindows("MainWindow");
            if (mn != null)
            {
                mn.Owner = null;
            }
            Window cf = getWindows("Config_frm");
            if (cf != null)
            {
                cf.Owner = null;
            }
            Window tf = getWindows("TC_frm");
            if (tf != null)
            {
                tf.Owner = null;
            }
            Window pf = getWindows("Preview_frm");
            if (pf != null)
            {
                pf.Owner = null;
                widen = ((Preview_frm)pf).widen;
            }

            if ((tf != null) && (pf != null) && (!widen))
             {
                 mn.Owner = pf;
                 pf.Owner = tf;
                 mn.Activate();
             }
             else if (tf != null)
             {
                 mn.Owner = tf;
                 mn.Activate();
             }else if ((cf != null) && (pf != null) && (!widen))
            {
                mn.Owner = cf;
                cf.Owner = pf;
                mn.Activate();
            }
            else if ((cf != null) && (pf != null) && (widen))
            {
                pf.Owner = mn;
                mn.Owner = cf;
                pf.Activate();
            }
            else if ((pf != null) && (widen))
            {
                pf.Owner = mn;
                pf.Activate();
            }
            else if ((pf != null) && (!widen))
            {
                mn.Owner = pf;
                mn.Activate();
            }
            else if (cf != null)
            {
                mn.Owner = cf;
                mn.Activate();
            }

        }*/


    }
}
